/*
 * acooly.cn Inc.
 * Copyright (c) 2017 All Rights Reserved.
 * create by ouwen
 * date:2017-09-22
 */
package com.yiji.fintech.musfinance.service.impl;

import com.acooly.core.common.service.EntityServiceImpl;
import com.acooly.core.utils.Money;
import com.google.common.collect.Maps;
import com.yiji.fintech.musfinance.dao.TransactionStatisticsDao;
import com.yiji.fintech.musfinance.entity.TransactionStatistics;
import com.yiji.fintech.musfinance.service.TransactionStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * center_transaction_statistics Service实现
 *
 * Date: 2017-09-22 14:45:07
 *
 * @author ouwen
 *
 */
@Service("transactionStatisticsService")
public class TransactionStatisticsServiceImpl extends EntityServiceImpl<TransactionStatistics, TransactionStatisticsDao> implements TransactionStatisticsService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<TransactionStatistics> statistic(TransactionStatistics statistics) {
        Map<String,Object> param = Maps.newHashMap();
        param.put("EQ_partnerId",statistics.getPartnerId());
        param.put("EQ_gatewayChannelApi",statistics.getGatewayChannelApi());
        param.put("EQ_instBankId",statistics.getInstBankId());
        if(!StringUtils.isEmpty(statistics.getSumDate())) {
            param.put("EQ_sumDate", statistics.getSumDate());
        }
        return getEntityDao().list(param,null);
    }
    @Override
    public int increment(String id,Money amount) {
        return jdbcTemplate.update("update center_transaction_statistics set money_sum = money_sum + ?, sum_cnt = sum_cnt + 1  WHERE id = ?",amount.getCent(),id);
    }
    @Override
    public int decrease(String id,Money amount) {
        return jdbcTemplate.update("update center_transaction_statistics set money_sum = money_sum - ?, sum_cnt = sum_cnt - 1 WHERE id = ? and money_sum > 0 and sum_cnt > 0",amount.getCent(),id);
    }
}
